﻿

using Cl.Entity.DataBaseEntity.User;
using Cl.Model.DataBaseEntityDtos.User.UserHistorys;
using Cl.Model.Results;
using Cl.Repository;
using SqlSugar;

namespace Cl.Service.Service.User.UserHistorys
{
    public class UserHistoryServer : IUserHistoryServer
    {
        private ClRepository<UserHistory> _userHistoryRepository;
        public UserHistoryServer(ClRepository<UserHistory> repository)
        {
            _userHistoryRepository = repository;
        }


        public PageResultModel<UserHistoryOutDto> QueryUserHistoryData(SearchUserHistoryDto request)
        {
            var response = new PageResultModel<UserHistoryOutDto>
            {
                Result = new List<UserHistoryOutDto>()
            };
            if (request.UserId <= 0)
                return response;

            int totalCount = 0;
            var sql = _userHistoryRepository.AsQueryable()
                .Where(x => x.UserId == request.UserId)
                .Includes(d => d.CreateUser);
            sql.OrderBy(p => p.Id, OrderByType.Desc);
            var data = sql.ToPageList(request.PageIndex, request.PageSize, ref totalCount)
                .Select(x => new UserHistoryOutDto(x) { }).ToList();
            response.TotalCount = totalCount;
            response.Result = data;
            return response;
        }

    }
}
